/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.openbrew.openbrewrf;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.annotations.CascadeOnDelete;

/**
 *
 * @author chapmand
 */
@Entity
@Table(name = "SalesOrder")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "SalesOrder.findAll", query = "SELECT s FROM SalesOrder s"),
    @NamedQuery(name = "SalesOrder.findBySOId", query = "SELECT s FROM SalesOrder s WHERE s.sOId = :sOId"),
    @NamedQuery(name = "SalesOrder.findBySODate", query = "SELECT s FROM SalesOrder s WHERE s.sODate = :sODate"),
    @NamedQuery(name = "SalesOrder.findBySOPaid", query = "SELECT s FROM SalesOrder s WHERE s.sOPaid = :sOPaid"),
    @NamedQuery(name = "SalesOrder.findByCheckNo", query = "SELECT s FROM SalesOrder s WHERE s.checkNo = :checkNo"),
    @NamedQuery(name = "SalesOrder.findByCheckNotes", query = "SELECT s FROM SalesOrder s WHERE s.checkNotes = :checkNotes"),
    @NamedQuery(name = "SalesOrder.findByCustomer", query = "SELECT s FROM SalesOrder s WHERE s.sOCust.cId = :cId"),
    @NamedQuery(name = "SalesOrder.findByExported", query = "SELECT s FROM SalesOrder s WHERE s.exported = :exported")})
public class SalesOrder implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "SOId")
    private Integer sOId;
    @Basic(optional = false)
    @NotNull
    @Column(name = "SODate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date sODate;
    @Basic(optional = false)
    @NotNull
    @Column(name = "SOPaid")
    private boolean sOPaid;
    @Basic(optional = false)
    @NotNull
    @Column(name = "CheckNo")
    private int checkNo;
    @Basic(optional = false)
    @NotNull
    @Size(min = 0, max = 100)
    @Column(name = "CheckNotes")
    private String checkNotes;
    @Basic(optional = false)
    @NotNull
    @Column(name = "exported")
    private boolean exported;
    @JoinColumn(name = "SOCust", referencedColumnName = "CId")
    @ManyToOne(optional = false)
    private Customer sOCust;
    @OneToMany(cascade=CascadeType.ALL, orphanRemoval = true)
    @CascadeOnDelete
    @JoinTable(
        name="JTSales",
        joinColumns={ @JoinColumn(name="SOId", referencedColumnName="SOID")},
        inverseJoinColumns={ @JoinColumn(name="SalesID", referencedColumnName="SId", unique=true)}
    )
    private List<Sales> salesCollection;
    @OneToMany(cascade=CascadeType.ALL, orphanRemoval = true)
    @CascadeOnDelete
    @JoinTable(
        name="JTISales",
        joinColumns={ @JoinColumn(name="SOId", referencedColumnName="SOID")},
        inverseJoinColumns={ @JoinColumn(name="iSaleID", referencedColumnName="iSaleID", unique=true)}
    )
    private List<InvSalesLines> invSalesLinesCollection;

    public SalesOrder() {
    }

    public SalesOrder(Integer sOId) {
        this.sOId = sOId;
    }

    public SalesOrder(Integer sOId, Date sODate, boolean sOPaid, int checkNo, String checkNotes, boolean exported) {
        this.sOId = sOId;
        this.sODate = sODate;
        this.sOPaid = sOPaid;
        this.checkNo = checkNo;
        this.checkNotes = checkNotes;
        this.exported = exported;
    }

    public Integer getSOId() {
        return sOId;
    }

    public void setSOId(Integer sOId) {
        this.sOId = sOId;
    }

    public Date getSODate() {
        return sODate;
    }

    public void setSODate(Date sODate) {
        this.sODate = sODate;
    }

    public boolean getSOPaid() {
        return sOPaid;
    }

    public void setSOPaid(boolean sOPaid) {
        this.sOPaid = sOPaid;
    }

    public Customer getSOCust() {
        return sOCust;
    }

    public void setSOCust(Customer sOCust) {
        this.sOCust = sOCust;
    }

    public int getCheckNo() {
        return checkNo;
    }

    public void setCheckNo(int checkNo) {
        this.checkNo = checkNo;
    }

    public String getCheckNotes() {
        return checkNotes;
    }

    public void setCheckNotes(String checkNotes) {
        this.checkNotes = checkNotes;
    }

    public boolean getExported() {
        return exported;
    }

    public void setExported(boolean exported) {
        this.exported = exported;
    }

    public List<Sales> getSalesCollection() {
        return salesCollection;
    }

    public void setSalesCollection(List<Sales> salesCollection) {
        this.salesCollection = salesCollection;
    }

    public List<InvSalesLines> getInvSalesLinesCollection() {
        return invSalesLinesCollection;
    }

    public void setInvSalesLinesCollection(List<InvSalesLines> invSalesLinesCollection) {
        this.invSalesLinesCollection = invSalesLinesCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (sOId != null ? sOId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof SalesOrder)) {
            return false;
        }
        SalesOrder other = (SalesOrder) object;
        if ((this.sOId == null && other.sOId != null) || (this.sOId != null && !this.sOId.equals(other.sOId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.openbrew.openbrewrf.SalesOrder[ sOId=" + sOId + " ]";
    }

    public Date getDueDate(final int term) {
            final Calendar cal = Calendar.getInstance();
            cal.setTime(this.sODate);
            cal.add(Calendar.DATE, term);
            return new Date(cal.getTime().getTime());
    }

    /**
     * Returns true if not paid and is past term time
     * 
     * @param term
     *            in days
     * @return
     */
    public boolean isOverDue(final int term) {
            final Date now = new Date(new java.util.Date().getTime());
            return (!this.sOPaid && now.after(getDueDate(term)));
    }

    public String getDisplayID() {
            final String mask = "i000000";
            final String ds = Integer.toString(this.sOId);
            return mask.substring(0, mask.length() - ds.length()) + ds;
    }
}
